package com.hookoins.controller;

import com.hookoins.model.entity.DemoEntity;
import com.hookoins.service.DemoService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Collections;
import java.util.Date;

/**
 * @author Yao
 */
@Controller
@RequiredArgsConstructor
public class DemoController {
    private final DemoService demoService;

    @GetMapping("test")
    @ResponseBody
    public String test1() {
        return "hello world: " + new Date();
    }

    @GetMapping("query/{id}")
    @ResponseBody
    public DemoEntity query(@PathVariable String id) {
        return demoService.query(id);
    }

    @GetMapping("query/{id}/title")
    @ResponseBody
    public String queryTitle(@PathVariable String id) {
        return demoService.queryTitle(id);
    }

    @GetMapping(value = "/show/{id}")
    public ModelAndView say(@PathVariable String id) {
        ModelAndView mv = new ModelAndView();
        DemoEntity query = demoService.query(id);
        mv.addObject("demo", query);
        mv.addObject(query);
        mv.addObject("demoList", Collections.singletonList(query));
        mv.setViewName("result");
        return mv;
    }

}
